<!--
 * @Author: zl
 * @Date: 2021-02-05 14:52:13
 * @LastEditTime: 2021-11-30 18:49:18
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue3-element-admin\src\views\layoutpages\system\Roles.vue
-->
<template>
    <div class="ve_container">
        <!-- 搜索 -->
        <el-form ref="queryForm" :inline="true" :model="params">
            <el-form-item label="店铺名称" prop="storeCode">
                <el-select clearable v-model="storeCode" placeholder="请选择">
                    <el-option
                        v-for="item in payStores"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    @click="onSubmit(params, getDataList)"
                >
                    {{ menus.search.name }}
                </el-button>
                <!--                <el-button @click="resetForm(queryForm, params, getDataList)">-->
                <!--                    重置-->
                <!--                </el-button>-->
            </el-form-item>
        </el-form>

        <!-- 列表 -->
        <ve-table
            :table="{
                data: tableData,
            }"
            :pagination="{
                onSizeChange: (val) =>
                    handleSizeChange(val, params, getDataList),
                onCurrentChange: (val) =>
                    handleCurrentChange(val, params, getDataList),
                currentPage: page,
                pageSize: pageSize,
                total: total,
            }"
        >
            <template #tool_bar>
                <el-button
                    v-permission="['add']"
                    size="mini"
                    type="primary"
                    @click="handleEdit(menus.add.name)"
                >
                    {{ menus.add.name }}
                </el-button>
            </template>
            <el-table-column
                prop="couponName"
                label="优惠券名称"
            ></el-table-column>
            <el-table-column prop="storeCode" label="适用门店">
                <template v-slot="{ row }">
                    <el-tag>
                        {{
                            payStores.filter(
                                (item) => item.code == row.storeCode
                            )[0].name
                        }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="minMoney" label="满减金额"></el-table-column>
            <el-table-column
                prop="payNums"
                label="优惠券折扣"
            ></el-table-column>
            <el-table-column prop="couponType" label="优惠券类型">
                <template v-slot="{ row }">
                    <el-tag>
                        {{
                            dicList.filter(
                                (item) => item.code == row.couponType
                            )[0].name
                        }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column
                prop="startTime"
                label="开始时间"
            ></el-table-column>
            <el-table-column prop="endTime" label="结束时间"></el-table-column>
            <el-table-column prop="status" label="优惠券状态">
                <template v-slot="{ row }">
                    <el-tag
                        :type="
                            row.status === 1
                                ? 'primary'
                                : row.status === 2
                                ? 'success'
                                : row.status === 3
                                ? 'error'
                                : 'info'
                        "
                    >
                        {{
                            status.filter((item) => item.id == row.status)[0]
                                .name
                        }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="150px">
                <template v-slot:default="{ row }">
                    <el-button
                        v-permission="['edit']"
                        @click.prevent="handleEdit(menus.edit.name, row)"
                        type="primary"
                        size="mini"
                    >
                        {{ menus.edit.name }}
                    </el-button>
                    <el-button
                        v-permission="['del']"
                        @click.prevent="handleDel(row.id)"
                        type="danger"
                        size="mini"
                    >
                        {{ menus.del.name }}
                    </el-button>
                </template>
            </el-table-column>
        </ve-table>
        <!-- 编辑组件 -->
        <pay-coupon-edit
            v-if="showDialog"
            :rowData="rowData"
            :title="dialogTitle"
            :showDialog="showDialog"
            @closeDialog="handelDialog($event)"
        />
    </div>
</template>
<script>
export default {
    data: () => ({
        description: "优惠券信息",
        menus: {
            search: { name: "查询" },
            add: { name: "添加" },
            edit: { name: "编辑" },
            del: { name: "删除" },
        },
    }),
};
</script>

<script setup>
import PayCouponEdit from "./components/payCouponEdit";
import { reactive, toRefs, ref, onMounted, getCurrentInstance } from "vue";
//*导入公共查询方法
import {
    onSubmit,
    handleSizeChange,
    handleCurrentChange,
} from "@/views/layoutpages/common";

const { proxy } = getCurrentInstance();

const rowData = ref(null);
const dialogTitle = ref("");
const showDialog = ref(false);
const tableData = ref([]);
const params = reactive({
    pageSize: 10,
    page: 1,
    total: 0,
    storeCode: null,
});
const dicList = ref([]);
const payStores = ref([]);
const { pageSize, page, total, storeCode } = toRefs(params);

const status = [
    { name: "新创建", id: 1 },
    { name: "已发布", id: 2 },
    { name: "已停止", id: 3 },
];
/**
 * @description: 获取表单类型
 * @param {*}
 * @return {*}
 */
const getDicList = async (dicCode) => {
    const { code, data } = await VE_API.system.dictionaryEntityByCode({
        dicCode: dicCode,
    });
    if (code == "00") {
        dicList.value = data;
    }
};

/**
 * @description: 获取字典项类型
 * @param {*}
 * @return {*}
 */
const getPayStores = async () => {
    const { code, data } = await VE_API.system.payStores();
    if (code == "00") {
        payStores.value = data;
    }
    payStores.value = data;
};

/**
 * @description: dialog事件
 * @param {*}
 * @return {*}
 */
const handelDialog = (e) => {
    showDialog.value = e;
    getDataList();
};

/**
 * @description:添加or编辑事件
 * @param {*}
 * @return {*}
 */
const handleEdit = (title, row = null) => {
    showDialog.value = true;
    dialogTitle.value = title;
    rowData.value = row;
};
/**删除行数据
 * @description:
 * @param {*}
 * @return {*}
 */
const handleDel = (id) => {
    proxy
        .$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "error",
        })
        .then(async () => {
            const { code } = await VE_API.system.payCouponDel([id]);
            if (code == "00") {
                getDataList();
            }
        })
        .catch(() => {
            proxy.$message({
                type: "info",
                message: "已取消删除",
            });
        });
};

/**
 * @description: 获取列表数据
 * @param {*}
 * @return {*}
 */
const getDataList = async () => {
    const { code, data } = await VE_API.system.payCouponList(params);
    if (code == "00") {
        const { pageSize, pageNum, total, list } = data;
        params.pageSize = pageSize;
        params.page = pageNum;
        params.total = total;
        tableData.value = list;
    }
};
onMounted(async () => {
    await getPayStores();
    await getDicList("yhqlx");
    await getDataList();
    // maxHeight(pagination, queryForm, toolBar, ve_max_height);
});
</script>

<style lang="scss" scoped></style>
